package test;

import anaydis.sort.Sorter;
import sort.benchmark.Benchmark;
import sort.data.IntegerDataSetGenerator;
import sort.impl.QuickSorterCutOff;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class QuickSortTestExcel {
    private WritableWorkbook workbook;
    private WritableSheet sheet;

    public void init() {
        try {
            workbook = Workbook.createWorkbook(new File("practica4.xls"));
            sheet = workbook.createSheet("First Sheet", 0);

            IntegerDataSetGenerator generator = new IntegerDataSetGenerator();
            Benchmark benchmark = new Benchmark();

            QuickSorterCutOff quickSorterCutOff = new QuickSorterCutOff();
            quickSorterCutOff.addSorterListener(benchmark);

            makeFirstColumn();

            quickSorterCutOff.setM(10);
            sheet.addCell(new jxl.write.Label(1, 0, "M = 10"));
            makeComparisonColumns(1,generator, benchmark, quickSorterCutOff);

            quickSorterCutOff.setM(15);
            sheet.addCell(new jxl.write.Label(2, 0, "M = 15"));
            makeComparisonColumns(2,generator, benchmark, quickSorterCutOff);

            quickSorterCutOff.setM(20);
            sheet.addCell(new jxl.write.Label(3, 0, "M = 20"));
            makeComparisonColumns(3,generator, benchmark, quickSorterCutOff);

            workbook.write();
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }

    private void makeFirstColumn() throws WriteException {
        sheet.addCell(new jxl.write.Label(0, 0, "N"));
        sheet.addCell(new jxl.write.Number(0, 1, 1000));
        sheet.addCell(new jxl.write.Number(0, 4, 10000));
        sheet.addCell(new jxl.write.Number(0, 8, 100000));
    }

    private void makeComparisonColumns(int sorterColumn, IntegerDataSetGenerator generator, Benchmark benchmark, Sorter sorter) throws WriteException {
        makeSorterColumns(sorterColumn, generator, sorter, benchmark, 1, 1000);
        makeSorterColumns(sorterColumn, generator, sorter, benchmark, 4, 10000);
        makeSorterColumns(sorterColumn, generator, sorter, benchmark, 8, 100000);
    }

    private void makeSorterColumns(int sorterColumn, IntegerDataSetGenerator generator, Sorter sorter, Benchmark benchmark, int row, int n) throws WriteException {
        List<Integer> randomList = generator.createRandom(n);
        long timeR1 = System.currentTimeMillis();
        sorter.sort(generator.getComparator(), randomList);
        long timeR2 = System.currentTimeMillis();
        sheet.addCell(new jxl.write.Number(sorterColumn, row, benchmark.getComparisons()));
        sheet.addCell(new jxl.write.Number(sorterColumn, row + 1, benchmark.getSwaps()));
        sheet.addCell(new jxl.write.Number(sorterColumn, row + 2, timeR2 - timeR1));
    }

    public static void main(String[] args) {
        QuickSortTestExcel quickSortTest = new QuickSortTestExcel();
        quickSortTest.init();
    }
}




